using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using AuthService.Domain.Security;

namespace AuthService.Application.Security;

/// <summary>
/// 安全事件仓储接口
/// </summary>
public interface ISecurityEventRepository : IRepository<SecurityEvent, Guid>
{
    /// <summary>
    /// 根据事件ID获取事件
    /// </summary>
    Task<SecurityEvent> GetByEventIdAsync(
        string eventId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据用户ID获取事件
    /// </summary>
    Task<List<SecurityEvent>> GetByUserIdAsync(
        Guid userId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据事件类型获取事件
    /// </summary>
    Task<List<SecurityEvent>> GetByEventTypeAsync(
        string eventType,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据时间范围获取事件
    /// </summary>
    Task<List<SecurityEvent>> GetByTimeRangeAsync(
        DateTime startTime,
        DateTime endTime,
        string eventType = null,
        Guid? userId = null,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据IP地址获取事件
    /// </summary>
    Task<List<SecurityEvent>> GetByIpAddressAsync(
        string ipAddress,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取未处理的事件
    /// </summary>
    Task<List<SecurityEvent>> GetUnprocessedEventsAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取高风险事件
    /// </summary>
    Task<List<SecurityEvent>> GetHighRiskEventsAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取失败事件
    /// </summary>
    Task<List<SecurityEvent>> GetFailedEventsAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 标记事件为已处理
    /// </summary>
    Task MarkEventsAsProcessedAsync(
        List<Guid> eventIds,
        string processedBy = null,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取事件统计
    /// </summary>
    Task<SecurityEventStatisticsDto> GetEventStatisticsAsync(
        DateTime? startTime = null,
        DateTime? endTime = null,
        Guid? userId = null,
        string eventType = null,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 清理旧事件
    /// </summary>
    Task<int> CleanupOldEventsAsync(
        DateTime beforeDate,
        CancellationToken cancellationToken = default);
}

/// <summary>
/// 风险评估仓储接口
/// </summary>
public interface IRiskAssessmentRepository : IRepository<RiskAssessment, Guid>
{
    /// <summary>
    /// 根据评估ID获取评估
    /// </summary>
    Task<RiskAssessment> GetByAssessmentIdAsync(
        string assessmentId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据用户ID获取评估
    /// </summary>
    Task<List<RiskAssessment>> GetByUserIdAsync(
        Guid userId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据用户ID和操作类型获取评估
    /// </summary>
    Task<List<RiskAssessment>> GetByUserIdAndActionTypeAsync(
        Guid userId,
        string actionType,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据IP地址获取评估
    /// </summary>
    Task<List<RiskAssessment>> GetByIpAddressAsync(
        string ipAddress,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取高风险评估
    /// </summary>
    Task<List<RiskAssessment>> GetHighRiskAssessmentsAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取被拒绝的评估
    /// </summary>
    Task<List<RiskAssessment>> GetRejectedAssessmentsAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取未处理的评估
    /// </summary>
    Task<List<RiskAssessment>> GetUnprocessedAssessmentsAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取过期的评估
    /// </summary>
    Task<List<RiskAssessment>> GetExpiredAssessmentsAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 标记评估为已处理
    /// </summary>
    Task MarkAssessmentsAsProcessedAsync(
        List<Guid> assessmentIds,
        string processedBy = null,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取风险评估统计
    /// </summary>
    Task<RiskAssessmentStatisticsDto> GetAssessmentStatisticsAsync(
        DateTime? startTime = null,
        DateTime? endTime = null,
        Guid? userId = null,
        string actionType = null,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 清理过期评估
    /// </summary>
    Task<int> CleanupExpiredAssessmentsAsync(
        CancellationToken cancellationToken = default);
}

/// <summary>
/// 安全事件统计DTO
/// </summary>
public class SecurityEventStatisticsDto
{
    /// <summary>
    /// 总事件数
    /// </summary>
    public int TotalEvents { get; set; }

    /// <summary>
    /// 成功事件数
    /// </summary>
    public int SuccessEvents { get; set; }

    /// <summary>
    /// 失败事件数
    /// </summary>
    public int FailedEvents { get; set; }

    /// <summary>
    /// 高风险事件数
    /// </summary>
    public int HighRiskEvents { get; set; }

    /// <summary>
    /// 未处理事件数
    /// </summary>
    public int UnprocessedEvents { get; set; }

    /// <summary>
    /// 按类型分组统计
    /// </summary>
    public Dictionary<string, int> EventsByType { get; set; } = new();

    /// <summary>
    /// 按风险级别分组统计
    /// </summary>
    public Dictionary<string, int> EventsByRiskLevel { get; set; } = new();

    /// <summary>
    /// 按日期分组统计
    /// </summary>
    public Dictionary<DateTime, int> EventsByDate { get; set; } = new();
}

/// <summary>
/// 风险评估统计DTO
/// </summary>
public class RiskAssessmentStatisticsDto
{
    /// <summary>
    /// 总评估数
    /// </summary>
    public int TotalAssessments { get; set; }

    /// <summary>
    /// 允许的评估数
    /// </summary>
    public int AllowedAssessments { get; set; }

    /// <summary>
    /// 拒绝的评估数
    /// </summary>
    public int RejectedAssessments { get; set; }

    /// <summary>
    /// 高风险评估数
    /// </summary>
    public int HighRiskAssessments { get; set; }

    /// <summary>
    /// 未处理评估数
    /// </summary>
    public int UnprocessedAssessments { get; set; }

    /// <summary>
    /// 平均风险分数
    /// </summary>
    public float AverageRiskScore { get; set; }

    /// <summary>
    /// 按操作类型分组统计
    /// </summary>
    public Dictionary<string, int> AssessmentsByActionType { get; set; } = new();

    /// <summary>
    /// 按风险级别分组统计
    /// </summary>
    public Dictionary<string, int> AssessmentsByRiskLevel { get; set; } = new();

    /// <summary>
    /// 按日期分组统计
    /// </summary>
    public Dictionary<DateTime, int> AssessmentsByDate { get; set; } = new();
}